package vic.sendreceive.utilities;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class StringUtilities {

	public static String appendString(Object data, String delimiter) {
		StringBuilder result = new StringBuilder();
		result.append(delimiter);
		result.append(data);
		return result.toString();
	}

	public static String booleanToString(boolean val) {
		return (val == true) ? "1" : "0";
	}

	public static boolean stringToBoolean(String val) {
		return val.equals("1");
	}
	
	public static Date sqlStringToDate(String val) {		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");		
		Date result = null;
		try {
			result = dateFormat.parse(val);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static Date stringToDate(String val) {		
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");		
		Date result = null;
		try {
			result = dateFormat.parse(val);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static String DateToString(Date val) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmssSSS");
		return dateFormat.format(val);
	}	
	public static String getOnlyDate(Date val){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		return dateFormat.format(val);
	}
	
	public static String formatDateStringClient(String val)
	{
		int year = Integer.parseInt(val.substring(0, 4));		
		int month = Integer.parseInt(val.substring(4, 6));		
		int date = Integer.parseInt(val.substring(6, 8));		
		int hour = Integer.parseInt(val.substring(8, 10));		
		int minute = Integer.parseInt(val.substring(10, 12));		
		int second = Integer.parseInt(val.substring(12, 14));		
		int milisecond = Integer.parseInt(val.substring(14, 17));		
		
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append(year);
		strBuilder.append(appendString(month, "-"));
		strBuilder.append(appendString(date, "-"));
		strBuilder.append(appendString(hour, " "));
		strBuilder.append(appendString(minute, ":"));
		strBuilder.append(appendString(second, ":"));
		strBuilder.append(appendString(milisecond, "."));
		
		return strBuilder.toString();
	}
	
	public static String arrayToString(String[] arr, String delimiter)
	{		
		String result = "";
		if (arr.length > 0)
		{
			for (int i = 0; i < arr.length - 1; i++)
			{	
				if (arr[i].equals("") == false)
				{
					if ((i + 1 == arr.length - 1) && arr[i+1].equals(""))
						result += arr[i];
					else
						result += arr[i] + delimiter;				
				}
			}
			result += arr[arr.length - 1];
		}
		return result;
	}
	public static String zipArrString(String data){			
		data = data.replace("[", "");
		data = data.replace("]", "");
		data = data.replaceAll(", ", ",");
		return data;
		
	}
	public static String toSqlStringDate(Date val) {		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(val);		
	}

	public static String getViewCupTime(Date cupStartTime, Date cupEndTime) {
		String result = "";
		Calendar calendar = GregorianCalendar.getInstance(); 
		calendar.setTime(cupStartTime);
		int startTime = calendar.get(Calendar.HOUR_OF_DAY);
		String dateString = getOnlyDate(cupStartTime);
		calendar.setTime(cupEndTime);
		int endTime = calendar.get(Calendar.HOUR_OF_DAY);
		String startTimeString = (startTime < 9) ? "0" + startTime : startTime+"";
		String endTimeString = (endTime < 9) ? "0" + endTime : endTime+"";
		result += startTimeString + "-" + endTimeString + "h " +dateString;
		return result;
	}
}
